import * as order from "../../api/order";
var appInst =  getApp();


// orderStatus: 1
// orderStatusDesc: "待付款"

Page({
    data: {
        tab_list:[{
            label:"售后申请",
            value:""
        },{
            label:"处理中",
            value:"ING"
        },{
            label:"申请记录",
            value:"END"
        }],
        tabIndex:0,
        currentPage:1,
        createTime:[],
        keyword:"",
        list:[],
        currentPage:1,
        hasNextPage:true,
        total:"",
        refundStatus:""
    },
    onShow(){

        let pages = getCurrentPages();
        let currentPage = pages[pages.length-1];
        let options = currentPage.options;

        let tabIndex = this.data.tab_list.findIndex((el)=>{
            return el.value == options.refundStatus;
        })

        this.setData({
            hasNextPage:true,
            total:"",
            currentPage:1,
            createTime:[],
            tabIndex:tabIndex == -1 ? 0: tabIndex,
            refundStatus:options.refundStatus||'',
            keyword:options.orderNo||"" 
        });

        this.get_order_list();
        
    },
    after_sale_list(){

        
    },
    get_order_list(){

        //if( this.data.refundStatus ){
            order.afterSaleList({
                currentPage:this.data.currentPage,
                pageSize:10,
                keyword:this.data.keyword
            })
            .then((res)=>{
    
                appInst.globalData.pageCanLoad = false;

                if( res.list && res.list.length > 0){
                    res.list.map((el)=>{
                        el.orderListNewDetailVo = el.afterSaleRecordDetailList;
                    })
                }
    
                this.setData({
                    list:this.data.currentPage == 1 ? res.list :[...this.data.list, ...res.list],
                    hasNextPage:res.hasNextPage,
                    total:res.total
                })
            })
        //}
        // else{
        //     order.getOrderList2({
        //         currentPage:this.data.currentPage,
        //         pageSize:10,
        //         keyword:this.data.keyword
        //     })
        //     .then((res)=>{
    
        //         appInst.globalData.pageCanLoad = false;
    
        //         this.setData({
        //             list:this.data.currentPage == 1 ? res.list :[...this.data.list, ...res.list],
        //             hasNextPage:res.hasNextPage,
        //             total:res.total
        //         })
        //     })
        // }
    },
    top_picker_confirm(e){

        this.setData({
            list:[],
            hasNextPage:true,
            total:"",
            currentPage:1,
            createTime:e.detail
        })

        this.get_order_list();
    },
    handle_tab_change(e){

        let type = e.detail;

        this.setData({
            list:[],
            hasNextPage:true,
            total:"",
            currentPage:1,
            // refundStatus: !type ? '' : type
        });

        this.get_order_list();

    },
    handle_top_search(e){
        this.setData({
            list:[],
            hasNextPage:true,
            total:"",
            currentPage:1,
            keyword:e.detail.value
        })

        this.get_order_list();
    },
    handle_to_detail(e){
        let { item } = e.currentTarget.dataset;
        wx.navigateTo({
            url: '/pages/after-detail/after-detail?&orderNo=' + item.afterSaleOrderNo
        });
    },
    handle_cancel_refund(e){

        let { item } = e.currentTarget.dataset;
        order.cancelAfterSale({
            afterSaleOrderNo:item.afterSaleOrderNo
        })
        .then((res)=>{
            wx.showToast({
                title: res,
                icon: 'none',
                image: '',
                duration: 1500,
                mask: true
            });
        })
    },
    onReachBottom(){
        if(this.data.hasNextPage){
            this.setData({
                currentPage: ++ this.data.currentPage
            })
            this.get_order_list();
        }
    },
    handle_refund_apply(e){
        let { item } = e.currentTarget.dataset;

        if( item.afterSaleFlag == "Y" ){
            wx.navigateTo({
                url: '/pages/after-apply/after-apply?orderNo=' + item.orderNo
            });
        }
    }
})
